const BASE_URL = "http://192.168.137.1:3003"; // 代理的api或者后端地址

// 请求,参数{url:'', data:{}, method:'POST'}
export const request = (options) => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中'
		});

		uni.request({
			url: BASE_URL + options.url,
			method: options.method || "GET",
			timeout: 10000, // 设置超时时间为5秒
			// header:{token: uni.getStorageSync('user') ? uni.getStorageSync('user').token : ''},
			// 后端需要的请求头
			header: {
				Authorization: uni.getStorageSync("token") || "",
			},
			Client: "APP",
			data: options.data || {},
			success: (res) => {
				const data = res.data;
				console.log(data.code)
				if (data.code != 200) {
					uni.showToast({
						icon:"error",
						title: "请求错误"
					})
					if (data.code === 404) {
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/login/login",
							});
						}, 2000)

					}
				}
				uni.hideLoading();
				resolve(data);
			},
			fail: (error) => {
				uni.showToast({
					icon: "error",
					title: "操作错误",
				});
				uni.hideLoading();
				reject(error);
			},
		});
	});
};